package com.sqsf.util;

import com.sqsf.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class UserMapperTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void before() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(UserMapper.class);

    }

    @After
    public void after() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void selectAll() {
        for (User user : mapper.selectAll()) {
            System.out.println(user);
        }
        System.out.println("-----------查询所有用户信息成功------------");
    }

    @Test
    public void selectByUser() {
        User u = new User();
        for (User user : mapper.selectByUser(u)) {
            System.out.println(user);
        }
        System.out.println("-----------查询用户信息成功------------");
    }

    @Test
    public void selectByUserOne() {
        User u = new User();
        for (User user : mapper.selectByUserOne(u)) {
            System.out.println(user);
        }
        System.out.println("-----------查询用户信息成功------------");
    }

    @Test
    public void updateUser() {
        User u = new User();
        u.setUid(3);
        u.setUsername("TEST");
        u.setPassword("123456");
        u.setPhone("13673888888");
        mapper.updateUser(u);
        System.out.println("-----------修改用户信息成功------------");
    }

    @Test
    public void trimTest() {
        mapper.trimTest();
    }

    @Test
    public void insertList() {
        List<User> list = new ArrayList<>();
        list.add(new User("王五", "123456", "136788888888"));
        list.add(new User("王五", "123456", "136788888888"));
        list.add(new User("王五", "123456", "136788888888"));
        list.add(new User("王五", "123456", "136788888888"));
        list.add(new User("王五", "123456", "136788888888"));
        list.add(new User("王五", "123456", "136788888888"));
        list.add(new User("王五", "123456", "136788888888"));
        list.add(new User("王五", "123456", "136788888888"));
        list.add(new User("王五", "123456", "136788888888"));
        mapper.insertList(list);
        System.out.println("-----------批量插入用户信息成功------------");
    }

    @Test
    public void deleteByArray() {
        int[] ids = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
        mapper.deleteByArray(ids);
        System.out.println("-----------批量删除用户信息成功------------");
    }
}